/*
 * Main.java
 *
 */

package da522A_lab4B_2011;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 *
 * @author Saeid Kazemi
 * Kristianstad University
 * February 2010
 */

public class Main {
    
    //declaration
    Scanner myScanner;
    int[] arr;
    QuickSort q = new QuickSort();
    
    
    /** Creates a new instance of Main */
    public Main() {
        
        //Initialization
        myScanner = new Scanner(System.in);
        arr = new int[500];
        writeToArrayFromFile("million.txt", arr);
        
    }//End of constructor
    
    public void myMainMethod(){
        while(true){
            printMenu();
            //Only accepts numbers in range of signed integers. It does not take care of entries other than those numbers.
            switch(myScanner.nextInt()){
                case 1:
                    q.QuickSort(arr, 0, arr.length-1);
                    System.out.println("I'll complete implement either merge or quick sort and call the sort method here");
                    break;
                    
                case 2:
                    printArray();
                    break;
                    
                case 3:
                    System.out.println("which number are you searching for?");
                    int key = myScanner.nextInt();
                    BinarySearch bs = new BinarySearch();
                    int position =bs.recBinSearch(arr, 0, arr.length-1, key);
                    System.out.println("The number is in " + position);
                    break;
                    
                case 4:
                    System.out.println("Bye...");
                    System.exit(0);
                    break;
                    
                default:
                    System.out.println("The input is out of range");
                    break;
            } //End of switch
        } //End of while loop
    } //End of myMain method
    
    
    private void printArray(){
        for(int c= 1;c<arr.length;c++){
            System.out.print(arr[c-1]+ "\t");
            if(c%15==0)
                System.out.println("");
        }
        
    } //End of printArray method
    
    
    public static void main(String[] args) {
        // TODO code application logic here
        Main m1 = new Main();
        m1.myMainMethod();
    }
    
    public void writeToArrayFromFile(String fileName, int[] inputArr) {
        int temp = inputArr.length;
        String s;
        try{
            BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            while (temp>0) {
                s = b.readLine();
                if (s == null) break;
                inputArr[inputArr.length-temp--] = Integer.parseInt(s);
            }//End of try
            b.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }  //End of catch
    }//End of writeToArrayFromFile method
    
    
    private  void printMenu() {
        
        System.out.println("\n**** Main menu ****");
        System.out.println("select a number between 1 to 3");
        System.out.println("1. Sort");
        System.out.println("2. Print out one array");
        System.out.println("3. Search for a number");
        System.out.println("4. Quit");
        
    } //End of printMenu method
    
    
}


